package chapter14;

import java.util.Random;

/**
 * @author : DuJiabao
 * @Date : 2021/2/24 22:24
 * @Project : thinking-in-java
 * @File : null.java
 * @Desc :
 */
class Initable {
  static final int staticFinal = 47;
  static final int staticFinal2 = ClassInitialization.rand.nextInt();  // 随机生成一个int

  static {
    System.out.println("Initializing Initable");
  }
}

class Initable2 {
  static int staticNonFinal = 147;

  static {
    System.out.println("Initializing Initable2");
  }
}

class Initable3 {
  static int staticNonFinal = 74;

  static {
    System.out.println("Initializing Initable3");
  }
}

public class ClassInitialization {
  public static Random rand = new Random(47);

  public static void main(String[] args) throws ClassNotFoundException {
    Class<Initable> initable = Initable.class;   // 不触发初始化操作
    System.out.println("After creating Initable ref");
    System.out.println(Initable.staticFinal);  // 不触发初始化操作，重点！！！
    System.out.println(Initable.staticFinal2); // 触发初始化操作
    System.out.println(Initable2.staticNonFinal); // 触发初始化操作
    System.out.println("After creating Initable3 ref");
    Class<?> initable3 = Class.forName("chapter14.Initable3"); // 触发初始化操作
  }
}
